#!/bin/bash
###############################################################################
# @用例ID: 20230526-161207-889073538
# @用例名称: bash_test
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件:系统正常运行，且安装bash
    assert_true yum install -y bash

	
    # @预置条件:
    return 0
}

do_test() {
    msg "this is do_test"

    # @测试步骤:1: 测试变量的赋值和使用
    # @预期结果:1: 变量被成功赋值和使用
    my_var="Hello, world!"
    if [ "$my_var" = "Hello, world!" ]; then
        echo "Variable assignment and usage test passed"
    else
        echo "Variable assignment and usage test failed"
        return 1
    fi

    # @测试步骤:2: 测试条件判断
    # @预期结果:2: 条件判断正确
    if [ 1 -eq 1 ]; then
        echo "Condition test passed"
    else
        echo "Condition test failed"
        return 1
    fi

    if [ 1 -ne 1 ]; then
        echo "Condition test failed"
        return 1
    else
        echo "Condition test passed"
    fi

    # @测试步骤:3: 测试循环结构
    # @预期结果:3: 循环执行正确
    counter=0
    while [ $counter -lt 5 ]; do
        counter=$((counter + 1))
    done
    if [ $counter -eq 5 ]; then
        echo "Loop test passed"
    else
        echo "Loop test failed"
        return 1
    fi

    # @测试步骤:4: 测试函数定义和调用
    # @预期结果:4: 函数被成功定义和调用
    my_function() {
        echo "This is a function"
    }
    if my_function | grep -q "This is a function"; then
        echo "Function test passed"
    else
        echo "Function test failed"
        return 1
    fi

    # @测试步骤:5: 测试文件和目录操作
    # @预期结果:5: 文件和目录操作执行正确
    if touch test_file.txt && [ -f test_file.txt ]; then
        echo "File test passed"
    else
        echo "File test failed"
        return 1
    fi

    if mkdir test_dir && [ -d test_dir ]; then
        echo "Directory test passed"
    else
        echo "Directory test failed"
        return 1
    fi

    # @测试步骤:6: 测试管道和重定向
    # @预期结果:6: 管道和重定向执行正确
    if echo "Hello, world!" | grep -q "Hello"; then
        echo "Pipeline test passed"
    else
        echo "Pipeline test failed"
        return 1
    fi

    if echo "Hello" > output.txt && cat output.txt | grep -q "Hello"; then
        echo "Redirection test passed"
    else
        echo "Redirection test failed"
        return 1
    fi

    return 0
}


tc_teardown() {
    msg "this is tc_teardown"
    rm -rfv "$g_tmpdir" || return 1

    # 删除在测试中创建的文件
    rm -fv test_file.txt
    rm -rfv test_dir
    rm output.txt

    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
